package com.zensun.common.utils.file;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileNameUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.StrUtil;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.ObjectMetadata;
import com.zensun.common.config.OssProperties;
import com.zensun.common.utils.spring.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Date;

@Slf4j
public class OssFileUtils {

    private static OssProperties ossProperties = SpringUtils.getBean(OssProperties.class);

    private static OSS ossClient;

    private static OSS getInstance() {
        if (ossClient == null) {
            synchronized (OssFileUtils.class) {
                if (ossClient == null) {
                    ossClient = new OSSClientBuilder().build(ossProperties.getEndpoint(),
                            ossProperties.getAccessKeyId(), ossProperties.getAccessKeySecret());
                }
            }
        }
        return ossClient;
    }

    /**
     * 上传
     */
    public static String upload(MultipartFile file) throws IOException {
        String uploadUrl = "";
        // getInstance().putObject(ossProperties.getBucketName(), fileName, file);
        // https://husbandry-test.oss-cn-beijing.aliyuncs.com/
        String filePath = extractFilename(file);

        OSS instance = getInstance();
        if (MimeTypeUtils.getIsImg(file.getContentType())) {
            //1.如果是图片类型就增加头信息
            //设置https头信息
            // meta设置请求头
            ObjectMetadata meta = new ObjectMetadata();
            meta.setContentType(MimeTypeUtils.getMetadata(file.getContentType()));
            instance.putObject(ossProperties.getBucketName(), filePath, file.getInputStream(), meta);
            log.info("oss上传图片成功，路径为：====>{}", filePath);
        } else {
            //2.如果不是图片类型不增加头信息
            instance.putObject(ossProperties.getBucketName(), filePath, file.getInputStream());
            log.info("oss上传文件成功，路径为：====>{}", filePath);
        }

        uploadUrl = ossProperties.getBucketDomainName() + filePath;
        return uploadUrl;
    }

    /**
     * 文件上传路径
     */
    public static final String extractFilename(MultipartFile file) {
        String fileName = file.getOriginalFilename();
        String extension = getExtension(file);
        fileName = ossProperties.getPrefix() + "/" + DateUtil.format(new Date(), "yyyy/MM/dd") + "/" + IdUtil.randomUUID() + "." + extension;
        return fileName;
    }

    /**
     * 获取文件名的后缀
     *
     * @param file 表单文件
     * @return 后缀名
     */
    public static final String getExtension(MultipartFile file) {
        String extension = FileNameUtil.extName(file.getOriginalFilename());
        if (StrUtil.isEmpty(extension)) {
            extension = MimeTypeUtils.getExtension(file.getContentType());
        }
        return extension;
    }

}
